package com.baibei.ebec.adapter;

import android.app.Activity;
import android.media.Image;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.webkit.JavascriptInterface;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.ImageView;
import android.widget.TextView;
import com.baibei.crossboarder.R;
import com.baibei.ebec.sdk.TokenInfo;
import com.baibei.model.UserInfo;
import com.baibei.module.AppRouter;
import com.bumptech.glide.Glide;
import com.rae.swift.session.SessionManager;
import com.umeng.socialize.utils.Log;

import java.util.ArrayList;
import java.util.Map;


/**
 * Created by tang on 2018/3/19.
 */

public class PopDialogAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> implements View.OnClickListener {
    private ArrayList<Map<String,String>> maplist;
    private Activity mActivity;
    private int TYPE_PAGE_1 = 1;
    private int TYPE_PAGE_OTHER = 2;

    public PopDialogAdapter(ArrayList<Map<String,String>> maplist,Activity mActivity){
        this.maplist = maplist;
        this.mActivity = mActivity;
    }

    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup viewGroup, int i) {
        if (i == TYPE_PAGE_1){
            View view = LayoutInflater.from(mActivity).inflate(R.layout.dialog_page_1, null);
            Page1ViewHolder page1ViewHolder = new Page1ViewHolder(view);
            page1ViewHolder.ivClose.setOnClickListener(this);
            return page1ViewHolder;
        }
        View view = LayoutInflater.from(mActivity).inflate(R.layout.dialog_page_2, null);
        ViewHolder vh = new ViewHolder(view);
        vh.tvState.setOnClickListener(this);
        vh.tvClick.setOnClickListener(this);
        vh.ivClose.setOnClickListener(this);
        return vh;
    }

    @Override
    public void onBindViewHolder(RecyclerView.ViewHolder viewHolder, int i) {
        Map<String, String> map = maplist.get(i);
        Log.i("pop",map.toString());

        if (i==0){
            WebView wv = ((Page1ViewHolder)viewHolder).wv;
            ImageView ivClose = ((Page1ViewHolder)viewHolder).ivClose;
            WebSettings settings = wv.getSettings();
            settings.setJavaScriptEnabled(true);
            settings.setLoadWithOverviewMode(true);
            settings.setBuiltInZoomControls(true);
            settings.setJavaScriptEnabled(true);
            settings.setUseWideViewPort(true);
            settings.setSupportZoom(true);
            settings.setJavaScriptCanOpenWindowsAutomatically(true);
            settings.setCacheMode(WebSettings.LOAD_DEFAULT);
            settings.setGeolocationEnabled(true);
            settings.setDomStorageEnabled(true);
            settings.setDatabaseEnabled(true);
            settings.setUseWideViewPort(true);
            settings.setAllowFileAccess(true);
            settings.setSupportZoom(true);
            settings.setLoadWithOverviewMode(true);
            settings.setPluginState(WebSettings.PluginState.ON);
            settings.setCacheMode(WebSettings.LOAD_NO_CACHE); // 不加载缓存内容
            wv.setWebViewClient(new WebViewClient(){
                @Override
                public boolean shouldOverrideUrlLoading(WebView view, String url) {
                    view.loadUrl(url);
                    return super.shouldOverrideUrlLoading(view, url);
                }
            });
            wv.addJavascriptInterface(new JiaoHu(),"android");
            Glide.with(mActivity).load(R.mipmap.ic_close).into(ivClose);
            wv.loadUrl(map.get("url"));
            ivClose.setTag(i);
            return;
        }

        TextView tvState = ((ViewHolder)viewHolder).tvState;
        ImageView ivBg = ((ViewHolder)viewHolder).ivBg;
        ImageView ivTicket = ((ViewHolder)viewHolder).ivTicket;
        ImageView ivClose = ((ViewHolder)viewHolder).ivClose;
        TextView tvClick = ((ViewHolder)viewHolder).tvClick;

        Glide.with(mActivity).load(map.containsKey("bjImg")?map.get("bjImg"):null).into(ivBg);
        Glide.with(mActivity).load(map.containsKey("ticketImg")?map.get("ticketImg"):null).into(ivTicket);
        Glide.with(mActivity).load(R.mipmap.ic_close).into(ivClose);

        if (map.containsKey("state")){
            tvState.setText("Y".equals(map.get("state"))?"已领取":"点击领取");
            tvState.setTextColor("Y".equals(map.get("state")) ? mActivity.getResources().getColor(com.baibei.ebec.home.R.color.green)
                    : mActivity.getResources().getColor(com.baibei.ebec.home.R.color.red));
        }

        tvState.setTag(i);
        tvClick.setTag(i);
        ivClose.setTag(i);
    }

    @Override
    public int getItemViewType(int position) {
        if (position==0){
            return TYPE_PAGE_1;
        }
        else {
            return TYPE_PAGE_OTHER;
        }
    }

    @Override
    public int getItemCount() {
        return maplist.size();
    }

    @Override
    public void onClick(View view) {
        if (mOnRecyclerViewItemClickListener != null){
            mOnRecyclerViewItemClickListener.onClick(view, (int) view.getTag());
        }

    }

    private onRecyclerViewItemClickListener mOnRecyclerViewItemClickListener = null;

    public class Page1ViewHolder extends RecyclerView.ViewHolder{

        private final WebView wv;
        private final ImageView ivClose;

        public Page1ViewHolder(View itemView) {
            super(itemView);
            wv = itemView.findViewById(R.id.wv);
            ivClose = itemView.findViewById(R.id.iv_close);
        }
    }
    public class ViewHolder extends RecyclerView.ViewHolder {

        private final ImageView ivBg;
        private final ImageView ivTicket;
        private final TextView tvState;
        private final TextView tvClick;
        private final ImageView ivClose;
        private final WebView wv;

        public ViewHolder(View itemView) {
            super(itemView);
            wv = itemView.findViewById(R.id.wv);
            ivBg = itemView.findViewById(R.id.iv_bg);
            ivTicket = itemView.findViewById(R.id.iv_ticket);
            tvState = itemView.findViewById(R.id.tv_state);
            tvClick = itemView.findViewById(R.id.tv_click);
            ivClose = itemView.findViewById(R.id.iv_close);
        }
    }

    public void setOnRecyclerViewItemClickListener(onRecyclerViewItemClickListener listener){
        this.mOnRecyclerViewItemClickListener = listener;
    }

    public interface onRecyclerViewItemClickListener{
        void onClick(View view,int position);
    }
    public class JiaoHu{
        @JavascriptInterface
        public void backHome(){
            AppRouter.routeToMain(mActivity,0);
        }

        @JavascriptInterface
        public void toShare(){
            AppRouter.routeToMain(mActivity,"share");
        }
        /**
         * 获取用户信息
         */

        @JavascriptInterface
        public String getUserInfo(){
            if (!SessionManager.getDefault().isLogin()){
                return "0";
            }
            TokenInfo userToken = SessionManager.getDefault().getUserToken();
            String accessToken = userToken.getAccessToken();
            return accessToken;
        }

        @JavascriptInterface
        public int getUserId(){
            if (!SessionManager.getDefault().isLogin()){
                return 0;
            }
            UserInfo userInfo = SessionManager.getDefault().getUser();
            return userInfo.getUserId();
        }

        @JavascriptInterface
        public void toQuan(){
            AppRouter.routeToTicket(mActivity);
        }

        @JavascriptInterface
        public void toLogin(){
            AppRouter.routeToLogin(mActivity);
        }

        @JavascriptInterface
        public void toChongZhi(){
            AppRouter.routeToRecharge(mActivity);
        }
    }
}
